package com.duoduo.
admin.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;

import com.duoduo.common.core.util.Page;
import com.duoduo.common.core.util.R;
import com.duoduo.common.jpa.service.CommonService;
import com.duoduo.admin.api.dto.SysRoleDTO;
import com.duoduo.admin.api.vo.RoleExcelVO;
import com.duoduo.admin.entity.SysRole;
import com.duoduo.admin.entity.SysRoleMenu;
import com.duoduo.admin.repository.SysRoleMenuRepository;
import com.duoduo.admin.repository.SysRoleRepository;

import jakarta.persistence.criteria.Predicate;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class SysRoleService extends CommonService<SysRoleDTO, SysRole, Long>{

	private final SysRoleRepository sysRoleRepository;
	
	private final SysRoleMenuRepository sysRoleMenuRepository;

	@Override
	public Specification<SysRole> getWhere(final SysRoleDTO request) {
		return (root, query, cb) -> {
			if(request==null) {
				return query.where().getRestriction();
			}
			final List<Predicate> predicate = new ArrayList<Predicate>();
			if(request.getId()!=null) {
				predicate.add(cb.equal(root.get("id"), request.getId()));	
			}		
			
			final Predicate[] pre = new Predicate[predicate.size()];
			return query.where(predicate.toArray(pre)).getRestriction();
		};
	}

	public List<SysRoleDTO> listRoles() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<RoleExcelVO> listRole() {
		// TODO Auto-generated method stub
		return null;
	}

	public R importRole(List<RoleExcelVO> excelVOList, BindingResult bindingResult) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<SysRoleDTO> pageMenus(Integer page, Integer pageSize, SysRoleDTO param) {
		org.springframework.data.domain.Page<SysRole> results = this.pageEnitry(page, pageSize, param);
		 List<SysRoleDTO> datas = results.map(p ->{
			List<Long> ids = p.getPermissions().stream().map(s ->s.getId()).toList();
			SysRoleDTO sysRoleDTO = new SysRoleDTO();
			BeanUtils.copyProperties(p, sysRoleDTO);
			sysRoleDTO.setMenuIds(ids);
			return sysRoleDTO;
		}).toList();
		 Page<SysRoleDTO> res = new Page<SysRoleDTO>();
		 res.setPageCount(results.getTotalPages());
		 res.setList(datas);
		 res.setTotal(results.getTotalElements());
		return res;
	}

	@Transactional
	public void createRole(SysRoleDTO request) {
		//保存角色
		  SysRole role = this.create(request);
		//保存菜单与角色关系
		 List<SysRoleMenu> list = request.getMenuIds().stream().map(p ->{
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setMenuId(p);
			sysRoleMenu.setRoleId(role.getId());
			return sysRoleMenu;
		}).toList();
		sysRoleMenuRepository.saveAll(list);
	}

	@Transactional
	public void updateRole(SysRoleDTO request) {
		this.update(request, request.getId());
		sysRoleMenuRepository.deleteByRoleId(request.getId());
		//保存菜单与角色关系
		 List<SysRoleMenu> list = request.getMenuIds().stream().map(p ->{
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setMenuId(p);
			sysRoleMenu.setRoleId(request.getId());
			return sysRoleMenu;
		}).toList();
		sysRoleMenuRepository.saveAll(list);
	}

}
